package kr.co.medical.controller;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import kr.co.medical.dao.DoctorDao;
import kr.co.medical.dao.NoticeDAO;
import kr.co.medical.dto.Cheobang_waiting_list;
import kr.co.medical.dto.Notice;
import kr.co.medical.dto.Wardlist;

import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class IndexController {

	@Autowired
	private SqlSession sqlSession;
	
	@RequestMapping("index.do")
	public String mainNotice(Model model) throws ClassNotFoundException,
			SQLException {
		NoticeDAO noticeDao = sqlSession.getMapper(NoticeDAO.class);
		List<Notice> list = noticeDao.mainNotice();
		model.addAttribute("list", list);
		DoctorDao doctordao = sqlSession.getMapper(DoctorDao.class);
		List<Wardlist> wardlist = doctordao.selectwardlistall();
        String yyMMdd = new SimpleDateFormat("yyyy-MM-dd").format(new Date()).toString();
		String MM = new SimpleDateFormat("MM").format(new Date()).toString();
		String dd = new SimpleDateFormat("dd").format(new Date()).toString();
		for(int i=0;i<wardlist.size(); i++){
			//월 구하기
			if(Integer.parseInt(wardlist.get(i).getWard_date().substring(5,7)) <= Integer.parseInt(MM)){
				int month=Integer.parseInt(MM)-Integer.parseInt(wardlist.get(i).getWard_date().substring(5,7));
				int warddate=Integer.parseInt(wardlist.get(i).getWard_date().substring(5,7));
				int maxmonth=doctordao.maxmonth(warddate);
				month=month*maxmonth;
				//입원 일수 구하는 if문
				int p=Integer.parseInt(dd)-Integer.parseInt(wardlist.get(i).getWard_date().substring(8,10));
				p=(month+p)+1;
				String w_soonap_id=wardlist.get(i).getW_soonap_id();
				doctordao.updateward_period(p,w_soonap_id);
			}
			
		}
		
		return "index.main";
	}

}
